// 云函数入口文件
const cloud = require('wx-server-sdk')
let superagent = require('superagent') // 发起请求获取页面html代码
let cheerio = require('cheerio') // 读取dom结构中的标签
let charset = require('superagent-charset') // 解决乱码
charset(superagent)
cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
    let server = `https://m.bige7.com/${event.url}`
    const result = await  superagent.get(server)// 拿到页面的html
    const $ = cheerio.load(result.text)

    let bookDetailData = {} // 本书详情
    bookDetailData['name'] = $('.book_info').find('.cover').find('img').attr('alt')
    bookDetailData['img'] = $('.book_info').find('.cover').find('img').attr('src')
    bookDetailData['author'] = $('.book_info').find('.book_box').find('.dd_box').eq(0).find('span').eq(0).text()
    bookDetailData['process'] = $('.book_info').find('.book_box').find('.dd_box').eq(1).find('span').eq(0).text()
    bookDetailData['new'] = $('.book_info').find('.book_box').find('.dd_box').eq(2).find('span')
    .text()
    bookDetailData['brief'] = $('.book_about').find('dt').text()
    bookDetailData['content'] = $('.book_about').find('dd').text()

    // 最新章节
    let Datalist = []
    const chapter = $('.book_last').find('dd').find('a')
    for(let i = 0;i<chapter.length;i++) {
        let obj = {}
        obj['url'] = $(chapter[i]).attr('href')
        obj['chapt'] = $(chapter[i]).text()
        Datalist.push(obj)
    }


    return {
        bookDetailData,
        Datalist
}}